<?php /** @var User $thatUser */ ?>
<div id="main">
    <div id="user-center-sidebar-left">
        <?php $this->load->view('home/user-center/sidebar'); ?>
    </div>
    <div id="user-center-body">
        <?php if (isset($CommodityFavArray)):; ?>
            <?php foreach ($CommodityFavArray as $thisCommodity): ?>
                <?php /** @var Commodity $thisCommodity */ ?>

                <div class="commodity" id="commodity_<?php echo $thisCommodity->getId() ?>">
                    <div class="commodity-image-block">
                        <div class="commodity-image">
                            <a href="<?php echo base_url('home/details/' . $thisCommodity->getId()) ?>"><img src="<?php echo $thisCommodity->getDisplayImageUrl() ?>" alt="商品图片" width="100%"></a>
                        </div>
                    </div>
                    <div class="commodity-content-block">
                        <div class="commodity-title-block">
                            <span class="text-primary commodity-name"><a href="<?php echo base_url('home/details/' . $thisCommodity->getId()) ?>"><?php echo $thisCommodity->getTitle() ?></a></span>

                            <span class="pull-right text-info commodity-time" style="font-size: 12px"><?php echo $thisCommodity->getReleaseTime() ?></span>
                        </div>
                        <div class="commodity-intro">
                            <?php echo $thisCommodity->getIntro() ?>
                        </div>
                    </div>
                    <div class="clearfix"></div>

                    <?php if($thatUser->getId() == $CurrentUser->getId()):?>
                        <div class="user-commodity-footer">
                            <div class="pull-left">
                                <a href="javascript:" class="commodity-toggle-favorite" data-commodity-id="<?php echo $thisCommodity->getId() ?>">❤ 取消收藏</a>
                            <?php if (isset($CurrentUser) && $CurrentUser->getId() > 0 && $thisCommodity->isCommodityPlusOneExistWithUserId($CurrentUser->getId())): ?>
                                <a href="javascript:" class="commodity-Praise-nums" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-thumbs-up"></span>&nbsp;已 赞（<?php echo $thisCommodity->getPlusOneCount()  ?>）</a>
                            <?php else : ?>
                                <a href="javascript:" class="commodity-Praise-nums" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-thumbs-up"></span>&nbsp;赞（<?php echo $thisCommodity->getPlusOneCount()  ?>）</a>
                            <?php endif; ?>
                            </div>
                            <div class="pull-right">
                                <span style="padding: 8px 15px;font-size: 12px;color: #828282; display:inline-block;">售价：<?php echo $thisCommodity->getDisplayPrice() ?></span>
                            </div>
                        </div>
                    <?php endif;?>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
        <div id="pagination">
            <?php if (isset($Pagination)) echo $Pagination; ?>
        </div>
    </div>
</div>
<script>
    $('.commodity-toggle-favorite').click(function () {
        //var _this = this;
        var commodityId = $(this).data('commodity-id');
        $.ajax({
            url: '<?php echo base_url('api/commodityToggleFavorite');?>',
            type: 'get',
            data: {commodity_id: commodityId},
            beforeSend: function () {
            },
            complete: function () {
            },
            success: function (jsonrsp) {
                try {
                    var rsp = $.parseJSON(jsonrsp);
                    switch (rsp.type) {
                        case 'success':
                            if (!rsp.data.isCommodityInFavoriteNow) {
                                $("#commodity_"+commodityId).hide(300);
                            }
                            break;
                        default:
                            break;
                    }
                } catch (ex) {
                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                    throw ex;
                }
            }
        });
    });
    $(document).on('click', '.commodity-Praise-nums', function () {
        var _this = this;
        var commodityId = $(this).data('commodity-id');
        $.ajax({
            url: '<?php echo base_url('api/commodityTogglePlusOne');?>',
            type: 'get',
            data: {commodity_id: commodityId},
            beforeSend: function () {
            },
            complete: function () {
            },
            success: function (jsonrsp) {
                try {
                    var rsp = $.parseJSON(jsonrsp);
                    switch (rsp.type) {
                        case 'success':
                            if (rsp.data.isCommodityPlusOneNow) {
                                $(_this).html('<span class="glyphicon glyphicon-thumbs-up"></span> 已 赞(' + rsp.data.countCommodityPlusOneNow + ')');
                            } else {
                                $(_this).html('<span class="glyphicon glyphicon-thumbs-up"></span> 赞(' + rsp.data.countCommodityPlusOneNow + ')');
                            }
                            break;
                        case 'error':
                            if (rsp.message == 'NOTLOGIN') {
                                showDialog('login');
                            }
                            break;
                        default:
                            break;
                    }
                } catch (ex) {
                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                    throw ex;
                }
            }
        });
    });
</script>